package com.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.MenuMapper;
import com.dk.mapper.RoleMenuMapper;
import com.dk.pojo.Menu;
import com.dk.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@org.springframework.stereotype.Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Map<String, Object>> getInfo(Integer pid) {
        List<Menu> list = menuMapper.getInfo(pid);
        String key = "reids:menuInfo";
        String feild = "feildMenu"+pid;
        List<Map<String, Object>> treeList =(List<Map<String, Object>>) redisTemplate.boundHashOps(key).get(key);
        if(treeList!=null && !treeList.isEmpty()){
            return treeList;
        }
        treeList = getMenuTree(list,0);
        redisTemplate.boundHashOps(key).put(feild,treeList);
        return treeList;
    }

    public List<Map<String,Object>> getMenuTree(List<Menu> menuList,Integer id){
        //容器
        List<Map<String,Object>> mapList = new ArrayList<>();
        //遍历结果集 menuList
        for (int i = 0; i < menuList.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = menuList.get(i);
            //对比 判断当前的父级节点是否为id
            if (id.equals(menu.getParentid())){
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("text",menu.getText());
                map.put("url",menu.getUrl());
                //递归调用
                map.put("nodes",getMenuTree(menuList,menu.getId()));
            }
            if (map != null){
                List<Map<String,Object>> list = (List<Map<String,Object>>) map.get("nodes");
                if(list.size() <= 0){
                    map.remove("nodes");
                }
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> getZtreeMenu() {
        List<Menu> menuList = menuMapper.selectByExample(null);
        List<Map<String, Object>> menuTree = getMenuZTree(menuList, 0);
        return menuTree;
    }

    public List<Map<String,Object>> getMenuZTree(List<Menu> menuList,Integer id){
        //容器
        List<Map<String,Object>> mapList = new ArrayList<>();
        //遍历结果集 menuList
        for (int i = 0; i < menuList.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = menuList.get(i);
            //对比 判断当前的父级节点是否为id
            if (id.equals(menu.getParentid())){
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("name",menu.getText());
                //递归调用
                map.put("children",getMenuZTree(menuList,menu.getId()));
            }
            if (map != null){
                List<Map<String,Object>> list = (List<Map<String,Object>>) map.get("children");
                if(list.size() <= 0){
                    map.remove("children");
                }
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public Integer insertInfo(Menu menu) {
        return menuMapper.insertSelective(menu);
    }

    @Override
    public Menu toUpdate(Integer parentid) {
        return menuMapper.selectByPrimaryKey(parentid);
    }

    @Override
    public boolean updateInfo(Menu menu) {
        Integer i = menuMapper.updateByPrimaryKeySelective(menu);
        return i > 0;
    }

    @Override
    public Integer deleteInfo(Integer parentid) {
        return menuMapper.deleteByPrimaryKey(parentid);
    }

    @Override
    public List<Map<String, Object>> getChecked(Integer roleId) {
        //查询所有
        List<Menu> menuList = menuMapper.selectByExample(null);
        //根据rid找到对应的菜单信息(mid)
        List<Integer> menuId = roleMenuMapper.selectMidsByRid(roleId);
        //拼接数据
        List<Map<String, Object>> menuZTreeChecked = getMenuZTreeChecked(menuList, 0, menuId);
        return menuZTreeChecked;
    }
    public List<Map<String,Object>> getMenuZTreeChecked(List<Menu> menuList,Integer id,List<Integer> menuId){
        //容器
        List<Map<String,Object>> mapList = new ArrayList<>();
        //遍历结果集 menuList
        for (int i = 0; i < menuList.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = menuList.get(i);
            //对比 判断当前的父级节点是否为id
            if (id.equals(menu.getParentid())){
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("name",menu.getText());
                //递归调用
                map.put("children",getMenuZTreeChecked(menuList,menu.getId(),menuId));
                if(menuId != null && menuId.size() > 0 && menuId.contains(menu.getId())){
                    map.put("checked",true);

                }
            }
            if (map != null){
                List<Map<String,Object>> list = (List<Map<String,Object>>) map.get("children");
                if(list.size() <= 0){
                    map.remove("children");
                }
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public List<String> selectPermsById(Integer pid) {
        return menuMapper.selectPermsById(pid);
    }
}
